package com.paomi.client.activity.search.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.fragment.first.model.TicketJson;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by pc on 2018/10/29
 * Describe:
 * com.paomi.client.fragment.first.adapter
 */
public class SearchTicketAdapter extends RecyclerView.Adapter<SearchTicketAdapter.ViewHolder> {

    private Activity mcontext;
    private List<TicketJson> ticketList = new ArrayList<>();

    public SearchTicketAdapter(Activity comtext) {
        mcontext = comtext;
    }

    public void setData(List<TicketJson> jsonList) {
        ticketList = jsonList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.item_first_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TicketJson ticketBean = ticketList.get(position);
        Util.setWidthAndHeight(holder.ivList, -1, (int) (Util.getDisplay(mcontext).widthPixels * 0.448 * 1.46));
        GlideApp.with(mcontext).asBitmap().load(ticketBean.getThumb()).placeholder(R.mipmap.img_default_goods).into(holder.ivList);//跳过硬盘缓存
        holder.titleList.setText(ticketBean.getName());
        holder.siteList.setText(ticketBean.getVenuename());
        holder.timeList.setText(ticketBean.getDate());
        holder.moneyList.setText(ticketBean.getPrice());
        if (ticketBean.isCouponFlag()) {
            holder.couponTxt.setVisibility(View.VISIBLE);
        } else {
            holder.couponTxt.setVisibility(View.GONE);
        }
        if (ticketBean.isIntegral()) {
            holder.accTxt.setVisibility(View.VISIBLE);
        } else {
            holder.accTxt.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mcontext, DetailTicketActivity.class);
                intent.putExtra("getId", "" + ticketBean.getId());
                mcontext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return ticketList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_list)
        ImageView ivList;
        @BindView(R.id.title_list)
        TextView titleList;
        @BindView(R.id.site_list)
        TextView siteList;
        @BindView(R.id.time_list)
        TextView timeList;
        @BindView(R.id.money_list)
        TextView moneyList;
        @BindView(R.id.money_txt)
        TextView moneyTxt;
        @BindView(R.id.acc_txt)
        TextView accTxt;
        @BindView(R.id.coupon_txt)
        TextView couponTxt;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
